<script setup>
import { useDictionary } from '@/use/use-dictionary'
import { status } from 'nprogress'

const dict = useDictionary(['task_risk_status', 'task_risk_category', 'task_risk_severity', 'task_risk_probability'])

const props = defineProps({
  modelValue: Boolean,
  projectId: String,
  ifAdd: Boolean, //是新增还是编辑
  projectMemberList: Array, // 获取项目成员
  formData: Object, // 表单数据
  ifSetting: Boolean, // 是否为设置
  view: Boolean,
})
const emit = defineEmits(['update:modelValue', 'changeFullScreen', 'changeLoading'])

const form = ref(props.formData)
const addForm = ref()
const rules = {
  riskName: [{ required: true, message: '请输入风险名称', trigger: 'blur' }],
  riskContent: [{ required: true, message: '请输入风险内容', trigger: 'blur' }],
  riskType: [{ required: true, message: '请选择风险类别', trigger: 'blur' }],
  status: [{ required: true, message: '请选择风险状态', trigger: 'blur' }],
  probability: [{ required: true, message: '请选择发生概率', trigger: 'blur' }],
  severity: [{ required: true, message: '请选择严重程度', trigger: 'blur' }],
  discoveryTime: [{ required: true, message: '请选择提出时间', trigger: 'blur' }],
  proposer: [{ required: true, message: '请选择提出人', trigger: 'blur' }],
}

const data1 = JSON.parse(JSON.stringify(props.formData))

const dict1 = computed(() => {
  const arr = []
  if (data1.status === 1) {
    dict.dictMap['task_risk_status'].forEach((item) => {
      if (
        Number(item.dictValue) === 1 ||
        Number(item.dictValue) === 2 ||
        Number(item.dictValue) === 3 ||
        Number(item.dictValue) === 4
      ) {
        arr.push(item)
      }
    })
    return arr
  } else if (data1.status > 1 && data1.status < 5) {
    dict.dictMap['task_risk_status'].forEach((item) => {
      if (Number(item.dictValue) !== 1) {
        arr.push(item)
      }
    })
    return arr
  } else if (data1.status >= 5) {
    dict.dictMap['task_risk_status'].forEach((item) => {
      if (Number(item.dictValue) === data1.status) {
        arr.push(item)
      }
    })
    return arr
  } else {
    return dict.dictMap['task_risk_status']
  }
})

defineExpose({
  addForm,
  form,
})
</script>

<template>
  <el-form ref="addForm" :model="form" :rules="rules" label-width="120px">
    <el-row>
      <el-col :span="24">
        <el-form-item label="风险名称" prop="riskName">
          <el-input
            v-model="form.riskName"
            placeholder="请输入风险名称"
            clearable
            :disabled="ifSetting || props.view"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item label="风险内容" prop="riskContent">
          <el-input
            v-model="form.riskContent"
            type="textarea"
            placeholder="请输入风险内容"
            clearable
            :disabled="ifSetting || props.view"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="风险类别" prop="riskType">
          <el-select v-model="form.riskType" placeholder="请选择风险类别" clearable :disabled="ifSetting || props.view">
            <el-option
              v-for="(item, index) in dict.dictMap['task_risk_category']"
              :key="index"
              :label="item.dictLabel"
              :value="Number(item.dictValue)"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="风险状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择风险状态" clearable :disabled="props.view">
            <el-option
              v-for="(item, index) in dict1"
              :key="index"
              :label="item.dictLabel"
              :value="Number(item.dictValue)"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="发生概率" prop="probability">
          <el-select v-model="form.probability" placeholder="请选择发生概率" clearable :disabled="props.view">
            <el-option
              v-for="(item, index) in dict.dictMap['task_risk_probability']"
              :key="index"
              :label="item.dictLabel"
              :value="Number(item.dictValue)"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="严重程度" prop="severity">
          <el-select v-model="form.severity" placeholder="请选择严重程度" clearable :disabled="props.view">
            <el-option
              v-for="(item, index) in dict.dictMap['task_risk_severity']"
              :key="index"
              :label="item.dictLabel"
              :value="Number(item.dictValue)"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="提出人" prop="proposer">
          <el-select
            v-model="form.proposer"
            placeholder="请选择提出人"
            clearable
            :disabled="!ifAdd || ifSetting || props.view">
            <el-option
              v-for="(item, index) in projectMemberList"
              :key="index"
              :label="item.userName"
              :value="Number(item.userId)"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="提出时间" prop="discoveryTime">
          <el-date-picker
            v-model="form.discoveryTime"
            type="datetime"
            placeholder="请选择提出时间"
            style="width: 100%"
            value-format="YYYY-MM-DD HH:mm:ss"
            :disabled="!ifAdd || ifSetting || props.view" />
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="确认人" prop="confirmedBy">
          <el-select
            v-model="form.confirmedBy"
            placeholder="请选择确认人"
            clearable
            :disabled="!ifAdd || ifSetting || props.view">
            <el-option
              v-for="(item, index) in projectMemberList"
              :key="index"
              :label="item.userName"
              :value="Number(item.userId)"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="确认时间" prop="confirmedTime">
          <el-date-picker
            v-model="form.confirmedTime"
            type="datetime"
            placeholder="请选择确认时间"
            style="width: 100%"
            value-format="YYYY-MM-DD HH:mm:ss"
            :disabled="!ifAdd || ifSetting || props.view" />
        </el-form-item>
      </el-col>
    </el-row>
    <el-row v-if="ifSetting">
      <el-col :span="24">
        <el-form-item label="应对措施" prop="solutions">
          <el-input
            v-model="form.solutions"
            type="textarea"
            placeholder="请输入应对措施"
            clearable
            :disabled="props.view"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>
